package com.xzlf.service;

import com.alibaba.fastjson.JSON;
import com.xzlf.config.Constans;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBlockingQueue;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.StringCodec;
import org.redisson.codec.JsonJacksonCodec;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class DemoService {

    @Autowired
    private RedissonClient redissonClient;

    public void add(Object obj) {
        RBlockingQueue<Object> blockingQueue = redissonClient.getBlockingQueue(Constans.BLOCKQUEUE, new JsonJacksonCodec());
        boolean add = blockingQueue.add(obj);
        log.info("=====queue添加结果:{}", add);

    }

    public Object pop() {
        RBlockingQueue<Object> blockingQueue = redissonClient.getBlockingQueue(Constans.BLOCKQUEUE, new JsonJacksonCodec());
        Object poll = null;
        if (blockingQueue.size() > 0) {
            poll = blockingQueue.poll();
            log.info("=====queue取出:{}", JSON.toJSON(poll));
        }
        return poll;

    }

    public int getBuffersize() {
        RBlockingQueue<Object> blockingQueue = redissonClient.getBlockingQueue(Constans.BLOCKQUEUE, new JsonJacksonCodec());
        return blockingQueue.size();
    }

    public void setStr(String str) {
        RBucket<Object> bucket = redissonClient.getBucket(Constans.FLAGKEY);
        bucket.set(str);
    }

    public String getStr() {
        RBucket<Object> bucket = redissonClient.getBucket(Constans.FLAGKEY);
        String str = (String) bucket.get();
        return str;
    }

    public void setResult(String key, String result) {
        RBucket<Object> bucket = redissonClient.getBucket(Constans.RESULT_KEY_PREFIX + key);
        bucket.set(result, 3600, TimeUnit.SECONDS);
    }

    public Object getResult(String key) {
        RBucket<Object> bucket = redissonClient.getBucket(Constans.RESULT_KEY_PREFIX + key);
        return bucket.get();
    }
}
